package cn.lxh.blog.controller;


import cn.lxh.blog.service.ILikesService;
import cn.lxh.util.JsonResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * LikesController控制器类
 */
@RestController // 该类交给spring管理, 并且返回JSON格式
@RequestMapping("/likes") // 默认请求路径
@Api(tags = "赞接口类")
public class LikesController {

    @Autowired
    private ILikesService likesService;

    /**
     * 根据ip判断是点赞还是取消点赞
     *
     * @param request
     * @return
     */
    @GetMapping("/getGoodByIp/{articleId}")
    public JsonResult getGoodByIp(HttpServletRequest request, @PathVariable("articleId") Long articleId) {
        try {
            likesService.getGoodByIp(request, articleId);
            return JsonResult.success(); // 成功
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error("网络超时!");
        }
    }

    /**
     * 决定点赞图标颜色状态
     *
     * @param request
     * @return
     */
    @GetMapping("/getLikeColor/{articleId}")
    public JsonResult getLikeColor(HttpServletRequest request, @PathVariable("articleId") Long articleId) {
        try {
            Integer good = likesService.findGoodByIp(request, articleId);
            if (good == 1) {
                return JsonResult.success("likes");
            } else {
                return JsonResult.success("no likes");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error("网络超时!");
        }
    }
}
